package com.ruoyi.project.cms.sa.customer.service;

import com.ruoyi.common.constant.CMSConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.sa.customer.domain.Customer;
import com.ruoyi.project.cms.sa.customer.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：MrL
 * 时间：2020-05-20
 */
@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> selectCustomerList(Customer customer) {
        return customerMapper.selectCustomerList(customer);
    }

    @Override
    public String checkCustomerNameUnique(Customer customer) {
        Long customerId = StringUtils.isNull(customer.getCid()) ? -1L : customer.getCid();
        Customer info = customerMapper.checkCustomerNameUnique(customer.getCname());
        if (StringUtils.isNotNull(info) && info.getCid().longValue() != customerId.longValue()) {
            return CMSConstants.CUSTOMER_NAME_NOT_UNIQUE;
        }
        return CMSConstants.CUSTOMER_NAME_UNIQUE;
    }

    @Override
    public int insertCustomer(Customer customer) {
        return customerMapper.insertCustomer(customer);
    }

    @Override
    public int deleteCustomerByIds(String ids) {
        Long[] customerIds = Convert.toLongArray(ids);
        return customerMapper.deleteCustomerByIds(customerIds);
    }

    @Override
    public Customer selectCustomerById(Long cid) {
        return customerMapper.selectCustomerById(cid);

    }

    @Override
    public int updateCustomer(Customer customer) {
        return customerMapper.updateCustomer(customer);
    }
}
